<template>
  <div class="calculator" id="app">
    <div class="screen">
      {{current}}
      <div class="flag">{{num0}} {{flag}}</div>
    </div>
    <div class="actions">
      <button @click="handleClear" class="actions-key">C</button>
      <button @click="handleFlagClick" class="actions-key">+</button>
      <button @click="handleFlagClick" class="actions-key">-</button>
      <button @click="handleFlagClick" class="actions-key">*</button>
      <button @click="handleFlagClick" class="actions-key">/</button>
    </div>
    <div class="buttons">
      <button @click="handleNumClick" class="buttons-key" v-for="(i) in 9" :key="i">{{i}}</button>
      <button @click="handleNumClick" class="buttons-key">0</button>
      <button @click="handleNumClick" class="buttons-key">.</button>
      <button @click="handleCalc" class="buttons-key flex2">=</button>
    </div>
  </div>
</template>

<script>
export default {
  name: "app",
  data() {
    return {
      flag: null,
      flagSwitch: false,
      num0: null,
      current: "0"
    };
  },
  methods: {
    handleClear() {
      this.current = 0;
      this.flag = null;
      this.num0 = null;
    },
    handleFlagClick(e) {
      let flag = e.target.innerText;
      this.flag = flag;
      this.flagSwitch = true;
    },
    handleNumClick(e) {
      let num = e.target.innerText;
      if (this.flagSwitch) {
        this.num0 = this.current;
        this.current = 0;
        this.flagSwitch = false;
      }
      if (this.current == 0) this.current = "";
      this.current += num;
    },
    handleCalc() {
      const num0 = Number(this.num0);
      const num1 = Number(this.current);

      if (this.flag === "+") {
        this.current = num0 + num1;
      } else if (this.flag === "-") {
        this.current = num0 - num1;
      } else if (this.flag === "*") {
        this.current = num0 * num1;
      } else if (this.flag === "/") {
        this.current = num0 / num1;
      }
      // this.num0 = null;
      // this.flag = null;
    }
  }
};
</script>

<style lang="stylus">
.calculator {
  width: 300px;
  border: 1px solid #000;
}

.screen {
  height: 100px;
  background: #ccc;
  display: flex;
  justify-content: flex-end;
  align-items: flex-end;
  padding: 10px;
  box-sizing: border-box;
  font-size: 28px;
  position: relative;

  .flag {
    position: absolute;
    color: red;
    right: 10px;
    top: 0;
  }
}

.actions {
  display: flex;
}

.actions-key {
  width: 25%;
  height: 60px;
  font-size: 20px;
}

.buttons {
  display: flex;
  flex-wrap: wrap;
}

.buttons-key {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 60px;
  text-align: center;
  width: 33.33%;
  font-size: 30px;

  // border 1px solid #f00
  // border-top: none
  // border-left none
  &.flex2 {
    flex: 1;
  }

  &:hover {
    // background #ccc
  }
}
</style>
